export function getPreviewUrl(fileName: string) {
  if (!fileName) {
    return '';
  }

  if (fileName.startsWith('http')) {
    return fileName;
  }

  return `${import.meta.env.VITE_OSS_PREFIX}/${fileName}`;
}

export function getThumbUrl(
  fileName: string,
  width: number = 120,
  height: number = 120
) {
  if (!fileName) {
    return '';
  }

  if (fileName.startsWith('http')) {
    return fileName;
  }

  return `${import.meta.env.VITE_OSS_PREFIX}/${fileName}?x-oss-process=image/resize,w_${width},h_${height}`;
}

export function getDownloadUrl(filename: string) {
  return (
    location.origin +
    import.meta.env.VITE_API_URL +
    import.meta.env.VITE_API_PREFIX +
    '/file/' +
    filename +
    '/download'
  );
}

export function transformThumbUrlToPreviewUrl(
  urls: string | string[]
): string | string[] {
  if (Array.isArray(urls)) {
    return urls.map((item) => item.split('?')[0]);
  }

  return urls.split('?')[0];
}
